package com.andatsoft.laisim.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;

public class DotStatusBar extends View {

	private int mPointColor = 0xffffffff;
	private int mSelectPointColor = 0xff000000;
	private int mLayoutWidth;
	private int mLayoutHeight;
	private float mPointSpace = 30f;
	private int mSelectedPointIndex = 0;
	private int mMax;

	private PointF[] mPoint;
	private PointF mSelectedPoint;

	private Paint mPointPainter;
	private Paint mSelectedPointPainter;

	public DotStatusBar(Context context) {
		super(context);
	}

	public DotStatusBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mLayoutWidth = w;
		mLayoutHeight = h;
		initPoints(mMax);
		setupPainters();
	}

	public void setupPainters() {

		if (mPointPainter == null)
			mPointPainter = new Paint();
		mPointPainter.setColor(mPointColor);
		mPointPainter.setAntiAlias(true);
		mPointPainter.setAlpha(75);

		if (mSelectedPointPainter == null)
			mSelectedPointPainter = new Paint();
		mSelectedPointPainter.setColor(mSelectPointColor);
		mSelectedPointPainter.setAntiAlias(true);
		mSelectedPointPainter.setAlpha(150);
	}

	private void initPoints(int max) {
		mPoint = new PointF[max];
		for (int i = 0; i < max; i++) {
			if (i == 0) {
				mPoint[i] = new PointF(mLayoutWidth / 2 - (mPointSpace * max)
						/ 2, mLayoutHeight);
			} else {
				mPoint[i] = new PointF(mPoint[i - 1].x + mPointSpace,
						mLayoutHeight);
			}
		}
		mSelectedPoint = new PointF(mPoint[0].x, mPoint[0].y);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		// draw point
		for (int i = 0; i < mPoint.length; i++) {
			canvas.drawCircle(mPoint[i].x, mLayoutHeight / 2, 5f, mPointPainter);
		}
		// draw selected point
		canvas.drawCircle(mSelectedPoint.x, mLayoutHeight / 2, 4f,
				mSelectedPointPainter);
	}

	public void setMaxPoint(int max) {
		this.mMax = max;
	}

	public void setSelectedPoint(int index) {
		if (index < 0 || index >= mPoint.length)
			return;
		mSelectedPointIndex = index;
		mSelectedPoint.x = mPoint[mSelectedPointIndex].x;
		invalidate();
	}
}